package com.future.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.validation.constraints.NotNull;

import com.future.common.annotation.ControllerMapping;
import com.future.common.annotation.NoNeedLogin;
import com.future.common.base.BaseController;
import com.future.common.global.PlatFormContext;
import com.future.common.utils.IDUtil;
import com.future.model.Metting;
import com.future.model.MettingPersons;
import com.future.model.Records;
import com.future.model.SysUser;
import com.future.requestbean.AddMettingBean;
import com.future.service.MettingService;
import com.jfinal.aop.Before;
import com.jfinal.aop.Inject;
import com.jfinal.kit.Kv;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.jfinal.upload.UploadFile;

@ControllerMapping("/metting")
public class MettingController extends BaseController{
	@Inject
	private MettingService mettingService;
	@Inject
	private SysUser sysUser;
	@Inject
	private Metting metting;
	@Inject
	private Records records;
	
	public void getMettingList(@NotNull(message = "请选择是否审核") String sfsh){
		renderResult(Kv.by("mettinglist", mettingService.getMettingList(sfsh)));
	}
	public void getMettingDetail(@NotNull(message="请选择会议") String mettingid){
		renderResult(Kv.by("metting",mettingService.getMettingDetail(mettingid)));
	}
	public void mettingsh(@NotNull(message="请选择会议") String mettingid){
		mettingService.mettingsh(mettingid);
		renderSuccess();
	}
	public void getMyMeetingList(@NotNull(message="请选择相应的类型")String type){
		renderResult(Kv.by("mettinglist", mettingService.getMyMeetingList(type,PlatFormContext.getCurrentUser().getUserid())));
	}
	@Before(Tx.class)
	public void addMetting(){
		AddMettingBean addMettingBean = getJsonBody(AddMettingBean.class);
		Metting metting = new Metting();
		String mettingid = addMettingBean.getMettingId();
		if(StrKit.isBlank(mettingid)){
			mettingid = IDUtil.getId();
		}
		metting.setTopic(addMettingBean.getTopic());
		metting.setDesc(addMettingBean.getDesc());
		metting.setFqr(PlatFormContext.getCurrentUser().getUserid());
		metting.setKssj(addMettingBean.getKssj());
		metting.setId(mettingid);
		metting.setCjsj(new Date());
		metting.setZlurl(addMettingBean.getZlurl());
		metting.save();
		for(String chr : addMettingBean.getChr()){
			MettingPersons mettingPersons = new MettingPersons();
			mettingPersons.setId(IDUtil.getId());
			mettingPersons.setMettingid(mettingid);
			mettingPersons.setUserid(chr);
			mettingPersons.setName(this.sysUser.findById(chr).getXm());
			mettingPersons.save();
		}
		renderSuccess();
	}
	@Before(Tx.class)
	@NoNeedLogin
	public void uploadZl(){
		UploadFile file = getFile();
		String fileName = file.getFileName();
		String id = IDUtil.getId();
		Map<String, String> result = new HashMap<String, String>();
		result.put("filename", fileName);
		result.put("id", id);
		renderResult(result);
	}
	@Before(Tx.class)
	public void saveSummary(@NotNull() String mettingid, @NotNull() String summary){
		this.metting.findById(mettingid).setHyzj(summary).update();
		renderSuccess();
	}
	
	public void getMessages(@NotNull() String mettingid){
		String fromdateString  = getPara("fromdate");
		Date fromdate= null;
		if(!StrKit.isBlank(fromdateString)){
			try {
				fromdate = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse(fromdateString);
			} catch (ParseException e) {
				fromdate = null;
			}
		}
		renderResult(mettingService.getMessages(mettingid,fromdate));
	}
	
}
